package weixin;

import java.text.SimpleDateFormat;
import java.util.Date;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angzh.bolibao.common.Util;
import com.angzh.bolibao.model.Brand;
import com.angzh.bolibao.model.Clicks;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;

public class WeixinController extends Controller {

	/**
	 * 获取用户openId
	 */
	public void getOpenId(){
		String code = getPara("code");
		String state = getPara("state");
		WebAccessToken webAccessToken = SnsUserInfoDemo.getWebAccessToken(code);
		if(webAccessToken != null){
			String openId = webAccessToken.getOpenid();
			setAttr("openId",openId);
			render("/openId.jsp");
		}else{
			System.out.println("获取access_token失败");
		}
	}
	
	public void paysucc(){
		render("/paysuccess.jsp");
	}
	
	public void payfail(){
		render("/payfail.jsp");
	}
	
	public void payfinish(){
		render("/payfinish.jsp");
	}
	/**
	 * 
	 * 购买页面
	 * 
	 **/
	public void index(){
		String code = getPara("code");
		String state = getPara("state");
		WebAccessToken webAccessToken = SnsUserInfoDemo.getWebAccessToken(code);
		if(webAccessToken != null){
			String openId = webAccessToken.getOpenid();
			setAttr("openId",openId);
//			render("/intoshangpin.jsp");
			render("/bolibao-detail.jsp");
			//redirect("/weixin/indexWithPara?openId="+openId);
		}else{
			System.out.println("获取access_token失败");
		}
//		render("/intoshangpin.jsp");
		render("/bolibao-detail.jsp");
		
	}
	//新需求商品购买页面
	public void goodsbuy(){
		
		List<Brand> brands = Brand.dao.findAll();
		setAttr("brands", brands);
		setAttr("brandsJson",JsonKit.toJson(brands));
//		render("/weixin/goods-buy.jsp");
		render("/index.jsp");
	}
	
	
	
	public void indexWithPara(){
		List<Brand> brands = Brand.dao.findAll();
		setAttr("brands", brands);
		setAttr("brandsJson",JsonKit.toJson(brands));
		
		String openId = getPara("openId")==null?"":getPara("openId");
		String code = getPara("code")==null?"":getPara("code");
		String state = getPara("state")==null?"":getPara("state");
		
		if(openId.equals("")&&!code.equals("")){
			WebAccessToken webAccessToken = SnsUserInfoDemo.getWebAccessToken(code);
			if(webAccessToken != null){
				openId = webAccessToken.getOpenid();
				setAttr("openId",openId);
				redirect("/weixin/indexWithPara?openId="+openId);
			}else{
				System.out.println("获取access_token失败");
			}

		}else if (openId.equals("")&&code.equals("")){
			redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx43fdd59a150361cc&redirect_uri=http%3a%2f%2fblb.zjhc.net.cn%2fweixin%2findexWithPara&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
		}else{
			if(!state.equals("worker")){
				redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx43fdd59a150361cc&redirect_uri=http%3a%2f%2fblb.zjhc.net.cn%2fweixin%2findexWithPara%3fopenId%3d"+openId+"&response_type=code&scope=snsapi_base&state=worker#wechat_redirect");
			}else{
				WebAccessToken webAccessToken = SnsUserInfoDemo.getWebAccessToken(code);
				if(webAccessToken != null){
					String useropenId = webAccessToken.getOpenid();
					if(useropenId!=null&&!useropenId.equals("")){
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						Date date = new Date();
						String createTime = sdf.format(date);
						Clicks clicks = new Clicks();
						clicks.set("openId", openId);
						clicks.set("createtime",createTime);
						clicks.save();
						setAttr("openId",useropenId);
						render("/index.jsp");
					}
					setAttr("useropenId",openId);
					//redirect("/weixin/indexWithPara?openId="+openId);
				}else{
					System.out.println("获取access_token失败");
				}
	//			try {
	//				String json = wxPayHelper.CreateBizPackage();
	//				setAttr("json",json);
	//			} catch (SDKRuntimeException e) {
	//				// TODO Auto-generated catch block
	//				e.printStackTrace();
	//			}
				
			}
		}
		
	}
	public void getPrepayId() throws Exception{
		Map<String, Object> retJson = Util.getRetJson();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmssSSSS");  
		Date date = new Date();
		String timeStr = dateFormat.format(date);
		String openId = getPara("openId");
		String body=getPara("body");
		String price = getPara("price");
		String nonce_str= CommonUtil.CreateNoncestr();
		String sign = "";
		HashMap<String,String> map = new HashMap<String,String>();
		WxPayHelper wxPayHelper = new WxPayHelper();
//		wxPayHelper.SetParameter("appid", "wx43fdd59a150361cc");
//		wxPayHelper.SetParameter("body",body);
//		wxPayHelper.SetParameter("mch_id","1235042102");
//		wxPayHelper.SetParameter("nonce_str",nonce_str);
//		wxPayHelper.SetParameter("notify_url","http://www.baidu.com");
//		wxPayHelper.SetParameter("openid",openId);
//		wxPayHelper.SetParameter("out_trade_no",timeStr);
//		wxPayHelper.SetParameter("spbill_create_ip","127.0.0.1");
//		wxPayHelper.SetParameter("total_fee",price);
//		wxPayHelper.SetParameter("trade_type","JSAPI");
		map.put("appid", "wx43fdd59a150361cc");
		map.put("body",body);
		map.put("device_info", "WEB");
		map.put("mch_id","1235042102");
		map.put("nonce_str",nonce_str);
		map.put("notify_url","http://www.baidu.com");
		map.put("openid",openId);
		map.put("out_trade_no",timeStr);
		map.put("spbill_create_ip","127.0.0.1");
		map.put("total_fee",price);
		map.put("trade_type","JSAPI");
		
		try {
			sign = wxPayHelper.GetBizSign(map,"zhejianghechengqichejituanyouxia");
		} catch (SDKRuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//先设置基本信息
		System.out.println("sign="+sign);
		Unifiedorder unifiedorder = new Unifiedorder();
		unifiedorder.setAppid("wx43fdd59a150361cc");
		unifiedorder.setBody(body);
		unifiedorder.setDevice_info("WEB");
		unifiedorder.setMch_id("1235042102");
		unifiedorder.setNonce_str(nonce_str);
		unifiedorder.setNotify_url("http://www.baidu.com");
		
		unifiedorder.setOpenid(openId);
		unifiedorder.setOut_trade_no(timeStr);
		unifiedorder.setSpbill_create_ip("127.0.0.1");
		unifiedorder.setTotal_fee(price);
		unifiedorder.setTrade_type("JSAPI");
		unifiedorder.setSign(sign);
		String prepay_id = wxPayHelper.CreateUnifiedorder(unifiedorder);
		System.out.println(prepay_id);
		retJson = Util.getOkRetJson("获取成功！");
		String timeStamp = Long.toString(new Date().getTime());
		retJson.put("prepay_id", "prepay_id="+prepay_id);
		retJson.put("appid", "wx43fdd59a150361cc");
		retJson.put("nonce_str", nonce_str);
		retJson.put("timeStamp", timeStamp);
		HashMap<String,String> map2 = new HashMap<String,String>();
		map2.put("appId", "wx43fdd59a150361cc");
		map2.put("nonceStr", nonce_str);
		map2.put("package", "prepay_id="+prepay_id);
		map2.put("signType", "MD5");
		map2.put("timeStamp", timeStamp);
		String paySign = wxPayHelper.createSign("appId=wx43fdd59a150361cc&nonceStr="+nonce_str+"&package=prepay_id="+prepay_id+"&signType=MD5&timeStamp="+timeStamp,"zhejianghechengqichejituanyouxia");
		retJson.put("paySign", paySign);
		renderJson(retJson);
	}
}